package lexer;

import java.util.Hashtable;

import token.Token;

public class Scope {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private final int level;
	private final Scope superScope;
	private Hashtable<String,Token> identifiers;
	
	public Scope() {
		identifiers = new Hashtable<String,Token>();
		superScope=null;
		level = 1;
	}
	
	public Scope(Scope superScope) { //superScope =  null is reserved only for the cases of separated scopes for example the constants scope and the main scope
		this.superScope=superScope;		
		if(superScope != null) {
			level = superScope.getLevel()+1;
		}else {
			superScope=null;
			level = 1;
		}
		identifiers = new Hashtable<String,Token>();
	}

	public int getLevel() {
		return level;
	}
	
	
	public Token isInThisScope(String tokenKey) {
		if((identifiers.get(tokenKey) == null) && (superScope != null)) {
			return superScope.isInThisScope(tokenKey);
		}
		return identifiers.get(tokenKey);
	}
		
	public void put(String key,Token value) {
		identifiers.put(key, value);
	}
	
	@Override
	public String toString() {
		return identifiers.toString();
	}
	
}
